/*
 * IMLocalStorageInternal.nc -- non-volatile storage interface
 *
 * Copyright (C) Andrey Vihrov <andrey.vihrov@gmail.com>, 2010
 * Copyright (C) Askar Salimbaev <ghostinshell@gmail.com>, 2010
 *
 * This file is part of Intermote.
 *
 * Intermote is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Intermote is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Intermote.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Storage.h"

interface IMLocalStorageInternal {
	/**
	 * Allocate block and initiate a write operation. On SUCCESS, the
	 * <code>writeDone</code> event will signal completion of the
	 * operation.
	 * <p>
	 *
	 * @param 'void* COUNT(len) buf' buffer to write data from.
	 * @param len number of bytes to write.
	 * @return
	 *   <li>SUCCESS if the request was accepted,
	 *   <li>EINVAL if the parameters are invalid
	 *   <li>FAIL if block allocation failed
	 */
	command error_t writeNew(void* buf, storage_len_t len);

	/**
	 * Initiate a write operation in specified block at specified offset. On SUCCESS, the
	 * <code>writeDone</code> event will signal completion of the
	 * operation.
	 * <p>
	 *
	 * @param 'uint16_t block' block number
	 * @param 'storage_addr_t offset' offset inside block
	 * @param 'void* COUNT(len) buf' buffer to write data from.
	 * @param len number of bytes to write.
	 * @return
	 *   <li>SUCCESS if the request was accepted,
	 *   <li>EINVAL if the parameters are invalid
	 *   <li>FAIL if write failed
	 */
	command error_t write(uint16_t block, storage_addr_t offset, void* buf, storage_len_t len);

	/**
	 * Signals the completion of a write operation.
	 *
	 * @param block number of block that data was writen to.
	 * @param 'void* COUNT(len) buf' buffer that written data was read from.
	 * @param len number of bytes written.
	 */
	event void writeDone(uint16_t block, void* buf, storage_len_t len);


	/**
	 * Initiate an volume erase operation. On SUCCESS, the
	 * <code>eraseDone</code> event will signal completion of the
	 * operation.
	 *
	 * @return
	 *   <li>SUCCESS if the request was accepted,
	 *   <li>EBUSY if a request is already being processed.
	 */
	command error_t eraseAll();

	/**
	 * Signals the completion of an erase operation.
	 *
	 * @param error SUCCESS if the operation was successful, FAIL if
	 *   it failed
	 */
	event void eraseDone(error_t error);


	/**
	 * Initiate a read operation within a block. On SUCCESS, the
	 * <code>readDone</code> event will signal completion of the
	 * operation.
	 *
	 * @param block block number
	 * @param addr offset inside the block
	 * @param 'void* COUNT(len) buf' buffer to place read data.
	 * @param len number of bytes to read.
	 * @return
	 *   <li>SUCCESS if the request was accepted,
	 *   <li>EINVAL if the parameters are invalid
	 *   <li>EBUSY if a request is already being processed.
	 */
	command error_t read(uint16_t block, storage_addr_t addr, void* buf, storage_len_t len);

	/**
	 * Signals the completion of a read operation.
	 *
	 * @param block number of readed block
	 * @param addr starting address of read.
	 * @param 'void* COUNT(len) buf' buffer where read data was placed.
	 * @param len number of bytes read.
	 */
	event void readDone(uint16_t block, storage_addr_t addr, void* buf, storage_len_t len);

	/**
	 * Initiate a sync operation to finalize writes to the volume. A
	 * sync operation must be issued to ensure that data is stored in
	 * non-volatile storage. On SUCCES, the <code>syncDone</code> event
	 * will signal completion of the operation.
	 *
	 * @return
	 *   <li>SUCCESS if the request was accepted,
	 *   <li>EBUSY if a request is already being processed.
	 */

	/**
	 * Report the usable size in blocks
	 * @return count of blocks.
	 */
	command uint16_t getSize();

	/**
	 * Initialize storage
	 */
	command void init();

}
